package com.uu.safeguard.controller;

import com.uu.safeguard.result.ResultFactory;
import com.uu.safeguard.utils.DbUtil;
import com.uu.safeguard.result.Result;
import org.apache.commons.lang3.StringUtils;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestHeader;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.regex.Pattern;

@RestController
@RequestMapping("/search")
public class SearchController {

    //列表
    @PostMapping("list")
    public Result list(@RequestHeader("token") String token, String keywords){

        if(null == keywords && StringUtils.isBlank(keywords)){
            return ResultFactory.buildFailResult("请输入关键词");
        }

        int limit = 5;

        List projectList = new ArrayList();
        int projectCount = 0;

        List stationList = new ArrayList();
        int stationCount = 0;

        List memberList = new ArrayList();
        int memberCount = 0;


        DbUtil db = new DbUtil();

        //小区
        List projectWhere = new ArrayList<Object>(){{
            add(new ArrayList(){{
                add("name");
                add("like");
                add("%" + keywords + "%");
            }});
        }};
        projectList = db.table("project").where(projectWhere).limit(limit).order("create_time desc").select("sn,name,address");
        projectCount = db.table("project").where(projectWhere).count("sn");


        //设备
        String stationSql = "select top " + limit + " sn,name,address from station where project_sn > 0  and ( sn = ? or name like ?) order by install_time desc,create_time desc";
        List stationParameter = new ArrayList();
        stationParameter.add(keywords);
        stationParameter.add("%" + keywords + "%");
        stationList = db.executeQueryBySql(stationSql, stationParameter);
        String stationCountSql = "select count(sn) as count from station where project_sn > 0  and ( sn = ? or name like ?)";
        List<Map> statioinCountList = db.executeQueryBySql(stationCountSql, stationParameter);
        stationCount = (int) statioinCountList.get(0).get("count");


        String memberSql = "select top " + limit + " sn,nickname,mobile,avatar,type from member where  ( sn = ? or mobile = ? or nickname like ?) order by create_time desc";
        List memberParameter = new ArrayList();
        memberParameter.add(keywords);
        memberParameter.add(keywords);
        memberParameter.add("%" + keywords + "%");

        String memberCountSql = "select count(sn) as count from member where  ( sn = ? or mobile = ? or nickname like ?)";

        //如果是六位数字 可能是卡号
        if(keywords.length() == 6 && Pattern.compile("^[-\\+]?[\\d]*$").matcher(keywords).matches()){
            memberSql = "select top " + limit + " sn,nickname,mobile,avatar,type from member where  ( sn = ? or mobile = ? or nickname like ? or sn like ?)  order by create_time desc";
            memberCountSql = "select count(sn) as count from member where  ( sn = ? or mobile = ? or nickname like ?  or sn like ?)";
            memberParameter.add("%" + String.format("%08d",Integer.parseInt(keywords)) + "%");
        }

        memberList = db.executeQueryBySql(memberSql, memberParameter);
        List<Map> memberCountList = db.executeQueryBySql(memberCountSql, memberParameter);
        memberCount = (int) memberCountList.get(0).get("count");


        Map data = new HashMap();
        data.put("projectList",projectList);
        data.put("projectCount",projectCount);
        data.put("stationList",stationList);
        data.put("stationCount",stationCount);
        data.put("memberList",memberList);
        data.put("memberCount",memberCount);
        return ResultFactory.buildSuccessResult(data);
    }
}
